package com.example.myapplication.activity;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.myapplication.R;

public class JinggaiActivity extends BaseActivity {

    @Override
    protected void doHttpData(Message message) {
        switch (message.what) {
            case GET_DEVICE_PROPERTIES:
                //设备属性回调
                //显示属性
                String string = (String) message.obj;
                Log.d("zoubo", string);
                String response = JSON.parseObject(string).getString("response");
                JSONObject properties =
                        JSON.parseObject(response).getJSONArray("services").getJSONObject(0).getJSONObject(
                                "properties");
                Log.d("zoubo", properties.toJSONString());
                //设置属性
                setData(properties);
                //开启定时轮询（查询设备属性）
                if (myScheduleTask == null) {
                    myScheduleTask = new MyScheduleTask();
                }
                getMyHttpHandler().postDelayed(myScheduleTask, 2000);
                break;
        }
    }

    /**
     * 设置属性数据
     *
     * @param properties
     */
    private void setData(JSONObject properties) {
        AnimatorSet set = new AnimatorSet();
        set.playTogether(setAnimText(value, properties.getString("Temperature")), setAnimText(value2,
                properties.getString("Accel_x")), setAnimText(value3, properties.getString("Accel_y")),
                setAnimText(value4, properties.getString("Accel_z")));
        set.start();
        //井盖状态
        String cover_status = properties.getString("Cover_Status");
        String display_cover_status = "未知";
        if (cover_status.equals("Level")) {
            display_cover_status = "水平";
        }
        if (cover_status.equals("Tilt")) {
            display_cover_status = "倾斜";
        }
        value5.setText(display_cover_status);
    }

    private ValueAnimator setAnimText(TextView target, String value) {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.0f, Float.valueOf(value)).setDuration(1000);
        valueAnimator.setDuration(1000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                target.setText(animation.getAnimatedValue().toString());
            }
        });
        return valueAnimator;
    }

    @Override
    protected String serviceID() {
        return "Manhole_Cover";
    }

    @Override
    protected int layoutID() {
        return R.layout.activity_jinggai;
    }

    private TextView value;
    private TextView value2;
    private TextView value3;
    private TextView value4;
    private TextView value5;

    @Override
    protected void initView() {
        value = (TextView) findViewById(R.id.value);
        value2 = (TextView) findViewById(R.id.value2);
        value3 = (TextView) findViewById(R.id.value3);
        value4 = (TextView) findViewById(R.id.value4);
        value5 = (TextView) findViewById(R.id.value5);
    }
}